<template>
    <div class="login_container">
        <div class="login_box">
            <img src="../assets/loginImg.webp" />
            <div class="topTitle">物流管理系统</div>
            <!-- 登录表单区 -->
            <el-form ref="loginFormRef" :model="loginForm" label-width="0px" class="login_form" :rules="loginFormRules">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" prefix-icon="iconfont icon-3702mima"></el-input>
                </el-form-item>
                <!-- 按钮区域 -->
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            // 登录表单的数据绑定对象
            loginForm: {
                username: 'admin',
                password: '123456',
            },
            //表单验证规则
            loginFormRules: {
                // 验证用户名是否合法
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    {
                        min: 3,
                        max: 10,
                        message: '长度在3 到 10个字符之间',
                        trigger: 'blur',
                    },
                ],
                // 验证密码是否合法
                password: [
                    { required: true, message: '请输入登录密码', trigger: 'blur' },
                    {
                        min: 6,
                        max: 15,
                        message: '长度在 6 到 15 个字符之间',
                        trigger: 'blur',
                    },
                ],
            },
        };
    },
    methods: {
        // 登录按钮
        login() {
            this.$refs.loginFormRef.validate(valid => {
                console.log(valid);
                if (!valid) return;
                this.$http
                    .post('login', {
                        username: this.loginForm.username,
                        password: this.loginForm.password,
                    })
                    .then(res => {
                        console.log(res.data);
                        if (res.data.meta.status != 200) {
                            this.$message({
                                message: res.data.meta.msg,
                                type: 'error',
                            });
                            return;
                        } else {
                            this.$message({
                                message: '登录成功',
                                type: 'success',
                                duration: 1000,
                            });
                            //登录成功之后的token，保存到客户端的sessionStorage中  localStorage是持久化存储机制，sessionStorage是会话期间的存储机制
                            console.log(res.data.data.token);
                            window.sessionStorage.setItem('token', res.data.data.token);
                            setTimeout(() => {
                                this.$router.push('/home');
                            }, 1000);
                        }
                    });
            });
        },
        // 点击重置按钮，重置登录表单
        resetLoginForm() {
            this.$refs.loginFormRef.resetFields();
        },
    },
};
</script>
<style lang="less" scoped>
.btns {
    display: flex;
    justify-content: flex-end;
}
</style>